package Fisica;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import javax.swing.JOptionPane;


public abstract class Conexion {
	static String user = "root";
	static String pass = "";
	static String ipPuertoDB = "localhost:3306/dbtoner";
	static String server = "Local";
	static String driver = "com.mysql.jdbc.Driver";
	static String jdbc = "jdbc:mysql://"+ipPuertoDB;
	
	public static Connection getConexion(){ //sql consulta a ejecutar.
		Connection cn = null;
		try{
			//retorna 0 si OK, de lo contrario -1
			Class.forName(driver).newInstance();
			
			cn = DriverManager.getConnection(jdbc, user, pass);
			if (cn.isClosed()){
				conectar();
				System.out.println("conecto");
			}
			
		}
		 catch (SQLException ex) {
			    // handle any errors
			    System.out.println("SQLException: " + ex.getMessage());
			    System.out.println("SQLState: " + ex.getSQLState());
			    System.out.println("VendorError: " + ex.getErrorCode());
	    }
		catch (Exception e){
			System.out.println(e.getMessage());
			e.printStackTrace();
			
		}
		return cn;
	}
	
	public static Connection conectar(){
	       Connection link = null;
	       try{
	           Class.forName(driver);
	           link = DriverManager.getConnection(jdbc, user, pass);
	       }catch(Exception ex){
	           JOptionPane.showMessageDialog(null, ex);
	       }
	       System.out.println("link conexion: "+link);
	       return link;
	   }
	
	public static String getJDBC(){
		return ipPuertoDB;
	}
	
	public static String getSERVER(){
		return server;
	}
	
	public static void main(String[] args) {
		Conexion cn = null;
		cn.getConexion();
		

	}

}
